package cn.edu.zafu.bigdata.Yum;

import cn.edu.zafu.bigdata.x.wupeng.g.ReverseThread;

import java.io.*;
public class RhymingWords {
    public static void main(String[] args) throws IOException {
        String rootdir = System.getProperty("user.dir");
        System.out.println("程序根目录："+rootdir);

        FileReader words=new FileReader(rootdir+"/files/words.txt");

        //进行单词的逆序、排序，在逆序还原
        Reader rhymingwords=reverse(sort(reverse(words)));

        //将处理后的单词列表输出显示
        BufferedReader in = new BufferedReader(rhymingwords);
        String input;
        while((input=in.readLine())!=null)
            System.out.println(input);
        in.close();
    }
    //创建管道，创建并启动单词逆序线程
    public static Reader reverse(Reader source)throws IOException{
        BufferedReader in = new BufferedReader(source);
        PipedWriter pipOut = new PipedWriter();
        PipedReader pipIn = new PipedReader(pipOut);
        PrintWriter out = new PrintWriter(pipOut);
        new ReverseThread(out,in).start();
        return pipIn;
    }
//    //创建管道，创建并启动单词排序线程
    public static Reader sort(Reader source)throws IOException{
        BufferedReader in = new BufferedReader(source);
        PipedWriter pipOut = new PipedWriter();
        PipedReader pipIn = new PipedReader(pipOut);
        PrintWriter out = new PrintWriter(pipOut);
        new SortThread(out,in).start();
        return pipIn;
   }
}
